import Vue from 'vue';
import Router from 'vue-router';
import { getLoginUserId } from "@/utils/userToken";

import Login from "@/views/login/index.vue";
import Index from "@/views/index.vue";
import Charts from "@/views/charts/index.vue";
import AddArticle from "@/views/article/addArticle.vue";
import ArticleList from "@/views/article";
import CategoryList from "@/views/category";
import UserList from "@/views/user";
import RoleList from "@/views/role";
import UserCenter from "@/views/system/user_center.vue";
import SystemConfig from "@/views/system/system_config.vue";
import SystemLog from "@/views/system/log.vue";
import ArticleApprove from "@/views/approve/article_approve.vue";
import LabelList from "@/views/label";
import Barrage from '../views/system/brrage.vue';
import ColumnApprove from '@/views/approve/column_approve.vue';

Vue.use(Router);

const router = new Router({
    routes: [
      {
        path: '/',
        name: 'Login',
        component: Login,
        meta: { title: '登录' }
      },
      {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { title: '登录' }
      },
      {
        path: '/home',
        name: 'Home',
        component: Index,
        iconCls: 'el-icon-tickets',
        meta: { title: '首页' },
        redirect: "/charts",
        children: [
          {
            path: '/charts',
            name: '首页',
            component: Charts,
            meta: { title: '首页' },
          },
          {
            path: '/article/list',
            name: '文章列表',
            component: ArticleList,
            meta: { title: '文章列表' },
          },
          {
            path: '/category/list',
            name: '分类列表',
            component: CategoryList,
            meta: { title: '分类列表' },
          },
          {
            path: '/label/list',
            name: '标签列表',
            component: LabelList,
            meta: { title: '标签列表' },
          },
          {
            path: '/article/addBlog',
            name: '发表文章',
            component: AddArticle,
            meta: { title: '发表文章' },
          },
          {
            path: '/user/list',
            name: '用户列表',
            component: UserList,
            meta: { title: '用户列表' },
          },
          {
            path: '/role/list',
            name: '角色列表',
            component: RoleList,
            meta: { title: '角色列表' },
          },
          {
            path: '/system/userCenter',
            name: '个人资料',
            component: UserCenter,
            meta: { title: '个人资料' },
          },
          {
            path: '/system/systemConfig',
            name: '系统配置',
            component: SystemConfig,
            meta: { title: '系统配置' },
          },
          {
            path: '/system/log',
            name: '日志管理',
            component: SystemLog,
            meta: { title: '日志管理' },
          },
          {
            path: '/approve/articleApprove',
            name: '文章审批',
            component: ArticleApprove,
            meta: { title: '文章审批' },
          },
          {
            path: '/approve/columnApprove',
            name: '专栏审批',
            component: ColumnApprove,
            meta: { title: '专栏审批' },
          },
          {
            path: '/system/barrage',
            name: '留言管理',
            component: Barrage,
            meta: { title: '留言管理' },
          }
        ]
      },


    ]



})

router.beforeEach((to, from, next) => {
  const userId = getLoginUserId();
  // 如果访问登录页面
  if (to.path === '/login' || to.path === '/') {
    if (userId === null || userId === '' || userId === undefined) {
      // 当未登录时，即认证token为空，允许访问登录页面
      next();
    } else {
      // 否则直接跳转到首页
      next('/home');
    }
  } else { // 如果访问非登录页面
    // 访问非登录页面  必须已经登录过 即cookie中存在认证token
    if (userId === null || userId === '' || userId === undefined) {
      console.log('没有令牌，回到登录页面...');
      next('/login');
    } else {
      next();
    }
  }
});

export default router;
